package com.kikyou.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kikyou.mapper.MenuMapper;
import com.kikyou.mapper.MenuRoleMapper;
import com.kikyou.pojo.Admin;
import com.kikyou.pojo.Menu;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kikyou.pojo.MenuRole;
import com.kikyou.pojo.RespBean;
import com.kikyou.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yuankaiqiang
 * @since 2022-03-05
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
    @Autowired(required = false)
    private MenuMapper menuMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired(required = false)
    private MenuRoleMapper menuRoleMap;
    /**
     * Description : TODO  通过用户id查询菜单列表<br/>
     * ChangeLog : 1. 创建 (2022/2/1 19:06 [ykq11]);
     **
     * *  @return null
     */
    @Override
    public List<Menu> getMenuByAdminId() {
        Integer adminId = ((Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
       ValueOperations<String, Object> valueOperations = redisTemplate .opsForValue();
        //从redis获取菜单数据
        List<Menu> menus = (List<Menu>) valueOperations.get("menu" + adminId);
        //如果为空，去数据库获取
        if(CollectionUtils.isEmpty(menus)){
            menus = menuMapper.getMenuByAdminId(adminId);
            //将数据设置到redis中
            valueOperations.set("menu" + adminId,menus);
        }
        return menus;
    }
    /**
     * Description : TODO  根据角色获得菜单列表<br/>
     * ChangeLog : 1. 创建 (2022/2/1 21:15 [ykq11]);
     * *  @return null
     */
    @Override
    public List<Menu> getMenusWithRole() {
        return menuMapper.getMenusWithRole();
    }

    /**
     * Description : TODO 查询所有菜单 <br/>
     * ChangeLog : 1. 创建 (2022/2/27 22:37 [ykq11]);
     * *
     * @return java.util.List<com.woniuxy.pojo.Menu>
     **/
    @Override
    public List<Menu> getAllMenus() {

        return menuMapper.getAllMenus();
    }
    /**
     * Description : TODO  更新角色ID<br/>
     * ChangeLog : 1. 创建 (2022/2/27 23:00 [ykq11]);
     ** @param rid
     * @param mids
     * @return com.woniuxy.pojo.RespBean
     **/
    @Override
    @Transactional
    public RespBean updateMenuRole(Integer rid, Integer[] mids) {
        menuRoleMap.delete(new QueryWrapper<MenuRole>().eq("rid",rid));
        if(null==mids||0==mids.length){
            return RespBean.success("更新成功");
        }
        menuRoleMap.insertRecord(rid,mids);
        return null;
    }
}
